//
//  WBVisitorLoginView.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/13.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit
import SnapKit
//在Swift中使用代理

@objc protocol WBVisitorLoginViewDelegate: NSObjectProtocol {
    //在协议中定义方法的声明，是必选的协议方法 默认必须实现 在Swift中不实现就默认报错
    
    //用户将要登录
    @objc optional func userWillLogin()
    //用户将要注册
     func userWillRegister()
    
    
}

class WBVisitorLoginView: UIView {
    //重构
    //声明属性 主要需要使用weak关键字来修饰
    weak var delegate: WBVisitorLoginViewDelegate?
    //MARK: 外界访问设置界面信息的方法
    func updateInfo(title: String, imageName: String, isHome: Bool = false) {
        //修改文字
        self.tipLabel.text = title
        //修改圈圈的图片
        self.circleView.image = UIImage(named: imageName)
        if isHome {
            //执行动画
            startAnimation()
        }else{
            //不是首页,隐藏🏠
            self.bigHouse.isHidden = true
            //隐藏遮盖
            self.backView.isHidden = true
        }
    }

    //动画效果
    private func startAnimation() {
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        //设置属性
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI * 2
        animator.duration = 20
        //isRemovedOnCompletion: 当动画效果啊完成之后就从图层移除,设置为false 就不会被移除图层, 默认值值是true
        animator.isRemovedOnCompletion = false
        //将动画添加到circleView的图层上
        circleView.layer.add(animator, forKey: nil)
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc private func loginBtnDidClick() {
        //调用代理方法 可选的协议方法
        delegate?.userWillLogin?()
    
    }
    
    @objc private func registerBtnDidClick() {
        //必选的协议方法
        delegate?.userWillRegister()
    }
    
    
    //MARK: 设置UI界面
   private func setupUI() {
        
        //监听按钮的点击事件
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)


        //视图层次，遮盖图 在上面添加
        addSubview(circleView)
        addSubview(backView)
        addSubview(bigHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)

    circleView.snp.makeConstraints { (make) in
        make.centerX.equalTo(self.snp.centerX)
        make.centerY.equalTo(self.snp.centerY).offset(-80)
    
    }
      bigHouse.snp.makeConstraints { (make) in
        make.center.equalTo(circleView)
        
    }
    tipLabel.snp.makeConstraints { (make) in
        make.top.equalTo(circleView.snp.bottom).offset(20)
        make.centerX.equalTo(circleView)
    }
    loginBtn.snp.makeConstraints { (make) in
        make.left.equalTo(tipLabel)
        make.top.equalTo(tipLabel.snp.bottom).offset(20)
        make.width.equalTo(100)
    }
    registerBtn.snp.makeConstraints { (make) in
        make.right.equalTo(tipLabel)
        make.width.equalTo(100)
        make.top.equalTo(tipLabel.snp.bottom).offset(20)
    }
    //遮盖视图的约束
    backView.snp.makeConstraints { (make) in
        make.center.equalTo(circleView)
    }
    //设置背景颜色
    backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)

    }
//实例化控件 懒加载 圈圈.房子.文字，描述
    
    lazy var circleView: UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    lazy var bigHouse: UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
   private lazy var tipLabel: UILabel = {
        let l = UILabel(title: "关注一些人,回到这里看看有什么惊喜", textCololr: UIColor.darkGray, fontSize: 14)
    //在这里设置行数  约束宽度
    l.numberOfLines = 0
    l.preferredMaxLayoutWidth = 220
    //文字居中
    l.textAlignment = .center
        return l
    }()
    //登录按钮 和 注册按钮
     lazy var loginBtn: UIButton = {
        
       let btn = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)

        return btn
        
        
    }()
    
     lazy var registerBtn: UIButton = {
        
        let btn = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)

        return btn
        
    }()
    
    //遮盖视图
    lazy var backView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    
    
}
